package cn.yunbamboos.utils;

import cn.hutool.core.net.NetUtil;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;

public class NetworkUtils {

    public static List<String>  getIPS(){
        List<String> ips = new ArrayList<>();
        LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(networkInterface -> {
            String name = networkInterface.getName();
            String displayName = networkInterface.getDisplayName();
            // 排除docker 虚拟机 vmware
            return !name.contains("docker")
                    && !name.contains("lo")
                    && !name.toLowerCase(Locale.ROOT).startsWith("vmware")
                    && !displayName.toLowerCase(Locale.ROOT).startsWith("vmware");
        }, address -> !address.isLoopbackAddress() && address instanceof Inet4Address && address.isSiteLocalAddress());
        for (InetAddress address : localAddressList) {
            ips.add(address.getHostAddress());
        }
        return ips;
    }
}
